package cn.zhangxin.pethome.mylistenter;

import cn.zhangxin.pethome.jwt.JwtRasHolder;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.FileCopyUtils;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import java.io.IOException;

@WebListener
@Slf4j
public class SystemInitContextListener implements ServletContextListener {
    @Value("${jwt.rsa.pub}")
    private String jwtRsaPub;
    @Value("${jwt.rsa.pri}")
    private String jwtRsaPrivate;


    @Override
    public void contextInitialized(ServletContextEvent sce) {
        try {
            log.info("System is initing!");
            JwtRasHolder.INSTANCE.setJwtRsaPubData(FileCopyUtils
                    .copyToByteArray(this.getClass().getClassLoader().getResourceAsStream(jwtRsaPub)));
            JwtRasHolder.INSTANCE.setJwtRsaPrivateData(FileCopyUtils
                    .copyToByteArray(this.getClass().getClassLoader().getResourceAsStream(jwtRsaPrivate)));
            log.info("System is init finished!");
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("获得jwt公钥和解密失败!");
            log.info("获得jwt公钥和解密失败!");
        }



    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {

        log.info("System is Destroyed!");
    }
}
